package org.example.servletpractice.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "GetCookieServlet",value = "/getCookieServlet")
public class GetCookieServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();

        //设置响应内容
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.println("<h1>获取的cookie值</h1>");

        for (Cookie cookie : cookies) {
            System.out.println("CookieName: "+cookie.getName()+",CookieValue: "+cookie.getValue());
            writer.println("<h3>CookieName: "+cookie.getName()+",CookieValue: "+cookie.getValue()+"</h3>");
        }
    }
}
